package com.aishua.fire.common;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DateUtil {

	public static final String YYYYMMDD_HH_MM_SS ="yyyy-MM-dd HH:mm:ss";
	public static final String YYYYMMDD_HH_MM_SS_ ="yyyyMMdd HH:mm:ss";
	public static final String YYYYMMDD_HH_MM="yyyy-MM-dd HH:mm";
	public static final String YYYYMM ="yyyyMM";
	public static final String YYYYMMDD ="yyyyMMdd";
	
	public static Date defaultConvert(String dateStr) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYYMMDD_HH_MM_SS);
		try {
			return simpleDateFormat.parse(dateStr);
		} catch (ParseException | NullPointerException e) {
			log.error("日期:{},格式错误:{}",dateStr,e);
		} 
		return null;
	}
	
	public static Date defaultConvert2(String dateStr) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYYMMDD_HH_MM);
		try {
			return simpleDateFormat.parse(dateStr);
		} catch (ParseException | NullPointerException e) {
			log.error("日期:{},格式错误:{}",dateStr,e);
		} 
		return null;
	}
	
	
	/**
	 * 按照格式解析转换
	 * @param dateStr
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date convertByFormat(String dateStr,String format) throws ParseException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
//		try {
			return simpleDateFormat.parse(dateStr);
//		} catch (ParseException e) {
//			log.error("日期:{},格式错误:{}",dateStr,e);
//		}
//		return null;
	}
	
	/**
	 * 获取当前年月时间
	 * @return
	 */
	public static String getNowYYYYMMStr() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYYMM);
		return simpleDateFormat.format(new Date());
	}
	
	public static String getCurrentDateStr() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYYMMDD_HH_MM_SS);
		return simpleDateFormat.format(new Date());
	}
	
	/**
	 * 日期转换成对应的字符串
	 * @param date
	 * @return
	 */
	public static String convertDate2Str(Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYYMMDD_HH_MM_SS);
		return simpleDateFormat.format(date);
	}
	
	/**
	 * 计算时间差
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long getDayDiff(Date d1,Date d2){
		long diff = d1.getTime() - d2.getTime();   
	    long days = Math.abs(diff / (1000 * 60 * 60 * 24));
	    return days;
	}
	
	public Timestamp parseTimestamp(String date) {
        String d = date;
        if (date.indexOf("/") > 0) {
            d = date.replace("/", "-");
        }
        String temp = d + " 00:00:00";
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time = df.parse(temp);
            return new Timestamp(time.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
	
	/*
	 * 日期增加天数
	 * */
	@SuppressWarnings("static-access")
	public static String addTianShu(String date,Integer tianShu) {
		 Calendar calendar = new GregorianCalendar(); 
	     calendar.setTime(defaultConvert(date)); 
	     calendar.add(calendar.DATE,tianShu);//把日期往后增加一天.整数往后推,负数往前移动 
	     Date endDate=calendar.getTime();   //这个时间就是日期往后推一天的结果 
	     String endDateStr = convertDate2Str(endDate);
		 return endDateStr;
	}
}
